package com.onlinevideo.util;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class MyBeanUtils {
    public static <T>T set(Class<T> type, Map map){
        T t = null;
        try {
            t = type.newInstance();
            BeanUtils.populate(t,map);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return t;
    }
/*    public static <T> T set(Class<T> tClass, Map<String, Object[]> map) throws IllegalAccessException, ClassNotFoundException, InstantiationException {
        T t = tClass.newInstance();
        Field[] declaredField = tClass.getDeclaredFields();
        for (Field field : declaredField) {
            String name = field.getName();
            Object[] objects = map.get(name);
            if (objects != null) {
                field.setAccessible(true);
                Class<?> type = field.getType();
                String s1 = type.toString();
                String substring = s1.substring(6);
                Class<?> aClass1 = Class.forName(substring);

                if (aClass1.equals(Integer.class)) {
                    field.set(t, Integer.valueOf(String.valueOf(objects[0])));
                } else if (aClass1.equals(String.class)) {
                    field.set(t, objects[0]);
                }
            }
        }
        return t;
    }*/
    public static <T> T setMap(Class<T> tClass, Map<String, Object> map) throws  IllegalAccessException, ClassNotFoundException, InstantiationException {
        T t = tClass.newInstance();
        fill(tClass,map,t);
        return t;
    }
    public static <T> void fill(Class<T> tClass, Map<String, Object> map,T t) throws  IllegalAccessException, ClassNotFoundException, InstantiationException {
        Field[] declaredField = tClass.getDeclaredFields();
        for (Field field : declaredField) {
            String name = field.getName();
            Object objects = map.get(name);
            if (objects != null) {
                field.setAccessible(true);
                Class<?> type = field.getType();
                String s1 = type.toString();
                String substring = s1.substring(6);
                Class<?> aClass1 = Class.forName(substring);

                if (aClass1.equals(Integer.class)) {
                    field.set(t, Integer.valueOf(String.valueOf(objects)));
                } else if (aClass1.equals(String.class)) {
                    field.set(t, objects);
                }else if (aClass1.equals(Long.class)){
                    field.set(t, Long.valueOf(String.valueOf(objects)));
                }
            }
        }
    }
    public static <T> void fillReq(Class<T> tClass, Map map,T t) throws  IllegalAccessException, ClassNotFoundException, InstantiationException {
        if (t == null) {
            return;
        }
        Field[] declaredField = tClass.getDeclaredFields();
        for (Field field : declaredField) {
            String name = field.getName();
            Object[] objects = (Object[]) map.get(name);
            if (objects != null) {
                field.setAccessible(true);
                Class<?> type = field.getType();
                String s1 = type.toString();
                String substring = s1.substring(6);
                Class<?> aClass1 = Class.forName(substring);

                if (aClass1.equals(Integer.class)) {
                    field.set(t, Integer.valueOf(String.valueOf(objects[0])));
                } else if (aClass1.equals(String.class)) {
                    field.set(t, objects[0]);
                }else if (aClass1.equals(Long.class)){
                    field.set(t, Long.valueOf(String.valueOf(objects[0])));
                }
            }
        }
    }
}
